<?php
class formModel extends baseModel{
    protected $table = 'form';

    //列表
    public function form_list() {
        return $this->select('','','id asc');
    }

    //获取表单信息
    public function table_info($table,$id=null) {
        $where="`table`='".$table."'";
        if(!empty($id)){
        $where.=' AND id<>'.$id;
        }
        return $this->find($where);
    }

    //获取表单信息
    public function info($id) {
        return $this->find('id='.$id);
    }

    //添加
    public function add($data) {
        //建立表单添加表
        $sql="
        CREATE TABLE IF NOT EXISTS `{$this->model->pre}form_data_{$data['table']}` (
          `id` int(10) NOT NULL AUTO_INCREMENT,
          PRIMARY KEY (`id`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
        ";
        $this->query($sql);
        return $this->insert($data);
    }

    //修改
    public function edit($data) {
        $info=$this->info($data['id']);
        //修改表单表
        $sql="
        ALTER TABLE {$this->model->pre}form_data_{$info['table']} RENAME TO {$this->model->pre}form_data_{$data['table']}
        ";
        $this->model->query($sql);
        //修改信息
        $condition['id']=intval($data['id']);
        return $this->update($condition,$data); 
    }

    //删除
    public function del($data)
    {
        $info=$this->info($data['id']);
        //删除表
        $sql="
        DROP TABLE `{$this->model->pre}form_data_{$info['table']}`
        ";
        $this->model->query($sql);
        //删除表内字段
        model('form_field')->form_del($data['id']);
        return $this->delete('id='.$data['id']); 
    }

}

?>